using Microsoft.AspNetCore.Mvc;
using JTZY.Model.Store.Dto;
using JTZY.Model.Store;
using JTZY.Service.Store.IStoreService;
using JTZY.Admin.WebApi.Filters;
using Aliyun.OSS;
using YKPrintService.VO;

//创建时间：2024-12-22
namespace JTZY.Admin.WebApi.Controllers.Store
{
    /// <summary>
    /// 打印机列表
    /// </summary>
    [Verify]
    [Route("store/Corecmsprinter")]
    public class CorecmsprinterController : BaseController
    {
        /// <summary>
        /// 打印机列表接口
        /// </summary>
        private readonly ICorecmsprinterService _CorecmsprinterService;
        public CorecmsprinterController(ICorecmsprinterService CorecmsprinterService)
        {
            _CorecmsprinterService = CorecmsprinterService;
        }

        /// <summary>
        /// 查询打印机列表列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        [HttpGet("list")]
        [ActionPermissionFilter(Permission = "corecmsprinter:list")]
        public IActionResult QueryCorecmsprinter([FromQuery] CorecmsprinterQueryDto parm)
        {
            var response = _CorecmsprinterService.GetList(parm);
            return SUCCESS(response);
        }


        /// <summary>
        /// 查询打印机列表详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet("{Id}")]
        [ActionPermissionFilter(Permission = "corecmsprinter:query")]
        public IActionResult GetCorecmsprinter(int Id)
        {
            var userId = HttpContext.GetUId();

            var response = _CorecmsprinterService.GetInfo(userId);
            
            var info = response.Adapt<CorecmsprinterDto>();
            return SUCCESS(info);
        }

        /// <summary>
        /// 添加打印机列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ActionPermissionFilter(Permission = "corecmsprinter:add")]
        [Log(Title = "打印机列表", BusinessType = BusinessType.INSERT)]
        public IActionResult AddCorecmsprinter([FromBody] CorecmsprinterDto parm)
        {
            var modal = parm.Adapt<Corecmsprinter>().ToCreate(HttpContext);
            modal.UserId = HttpContext.GetUId();
            modal.CreateTime = DateTime.Now;
            var res=XpsdkApi.addPrinters(new AddPrinterRequestItem { Sn = parm.PrinterName, Name = modal.UserId.ToString() });
            if (res.Code != 0) 
            {
                throw new CustomException(res.Msg); 
            }
            var response = _CorecmsprinterService.AddCorecmsprinter(modal);

            return SUCCESS(response);
        }

        /// <summary>
        /// 测试打印机
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        /// <exception cref="CustomException"></exception>
        [HttpPost("testprinter")]
        public IActionResult Testprinter([FromBody] CorecmsprinterDto parm ) 
        {
            var res=XpsdkApi.printFontAlign(parm.PrinterName);
            if (res.Code != 0) 
            {
                throw new CustomException(res.Msg);

            }
            return SUCCESS(res.Data);
        }

        /// <summary>
        /// 更新打印机列表
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        [ActionPermissionFilter(Permission = "corecmsprinter:edit")]
        [Log(Title = "打印机列表", BusinessType = BusinessType.UPDATE)]
        public IActionResult UpdateCorecmsprinter([FromBody] CorecmsprinterDto parm)
        {
            var modal = parm.Adapt<Corecmsprinter>().ToUpdate(HttpContext);

            var res = XpsdkApi.addPrinters(new AddPrinterRequestItem { Sn = parm.PrinterName, Name = modal.UserId.ToString() });
            if (res.Code != 0)
            {
                throw new CustomException(res.Msg);
            }
            var response = _CorecmsprinterService.UpdateCorecmsprinter(modal);

            return SUCCESS(response);
        }

        /// <summary>
        /// 删除打印机列表
        /// </summary>
        /// <returns></returns>
        [HttpPost("delete/{ids}")]
        [ActionPermissionFilter(Permission = "corecmsprinter:delete")]
        [Log(Title = "打印机列表", BusinessType = BusinessType.DELETE)]
        public IActionResult DeleteCorecmsprinter([FromRoute]string ids)
        {
            int id = !string.IsNullOrEmpty(ids) ? Convert.ToInt32(ids) : 0;
            if(id==0) throw new CustomException("没有打印删除");

            var printer=_CorecmsprinterService.GetById(id);
            var res = XpsdkApi.delPrinters(printer.PrinterName);
            if (res.Code != 0)
            {
                throw new CustomException(res.Msg);
            }

            var idArr = Tools.SplitAndConvert<int>(ids);

            return ToResponse(_CorecmsprinterService.Delete(idArr));
        }

    }
}